package com.tiger.log.core.aop.auth;

import com.tiger.log.common.entity.SysAuthLog;
import com.tiger.log.common.enums.AuthType;
import com.tiger.log.core.processing.auth.AuthProcessing;
import com.tiger.log.server.service.SysAuthLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @ClassName LogoutAop
 * @Description TODO
 * @Author huxuehao
 **/
@Aspect
@Component
public class LogoutAop {

    private final AuthProcessing authProcessing;
    private final SysAuthLogService sysAuthLogService;

    public LogoutAop(AuthProcessing authProcessing, SysAuthLogService sysAuthLogService) {
        this.authProcessing = authProcessing;
        this.sysAuthLogService = sysAuthLogService;
    }

    @Pointcut("@annotation(com.tiger.log.common.annotation.auth.LogoutCollect)")
    public void logoutPointCut() {

    }

    @Around("logoutPointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        if (authProcessing.isIllegal(joinPoint, AuthType.LOGOUT)) {
            return joinPoint.proceed();
        }
        SysAuthLog authLog = new SysAuthLog();
        Object res = null;
        try {
            /* 执行前的处理 */
            authProcessing.proceedPreProcessing(authLog, joinPoint, AuthType.LOGOUT);
            /* 执行 */
            res = joinPoint.proceed();
            /* 执行后的处理 */
            authProcessing.proceedPostProcessing(authLog, res, false);
        } catch (Throwable e) {
            /* 执行出现异常后的处理 */
            authProcessing.proceedPostProcessing(authLog, res, true);
            throw e;
        } finally {
            sysAuthLogService.asyncGenLog(authLog);
        }
        return res;
    }
}
